package com.changgou.service.goods.service.impl;



import com.changgou.goods.pojo.Brand;
import com.changgou.service.goods.mapper.BrandMapper;
import com.changgou.service.goods.service.BrandService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;

@Service
public class BrandServiceImpl implements BrandService {
    @Autowired
    private BrandMapper brandMapper;

    /**
     * 查询所有
     */
    @Override
    public List<Brand> findAll() {
        return brandMapper.selectAll();
    }

    /**
     * 根据id查询
     */
    @Override
    public Brand findAById(Integer id) {
        return brandMapper.selectByPrimaryKey(id);
    }

    /**
     * 新增品牌
     *
     * @param brand
     */
    @Override
    public void add(Brand brand) {
        brandMapper.insertSelective(brand);
    }

    /**
     * 修改品牌
     *
     * @param brand
     */
    @Override
    @Transactional //事务管理
    public void update(Brand brand) {
        brandMapper.updateByPrimaryKeySelective(brand);
    }

    /**
     * 删除品牌
     *
     * @param id
     */
    @Override
    @Transactional
    public void delete(Integer id) {
        brandMapper.deleteByPrimaryKey(id);
    }

    /**
     * 多条件查询
     *
     * @param map
     */
    @Override
    public List<Brand> findList(Map<String, Object> map) {
        Example example = new Example(Brand.class);
        Example.Criteria criteria = example.createCriteria();
        if (map != null) {
            //品牌名称(模糊查询)
            if (map.get("name") != null && !"".equals(map.get("name"))) {
                criteria.andLike("name", "%" + map.get("name") + "%");
            }
            //品牌的首字母查询(精确查询)
            if (map.get("letter") != null && !"".equals(map.get("letter"))) {
                criteria.andEqualTo("letter", map.get("letter"));
            }
        }
        return brandMapper.selectByExample(example);
    }

    /**
     * 分页查询
     *
     * @param page
     * @param size
     */
    @Override
    public Page<Brand> findPage(int page, int size) {
        PageHelper.startPage(page,size);
        return (Page<Brand>) brandMapper.selectAll();
    }

}
